package com.heima.wemedia.service.impl;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.WemediaConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelSaveDto;
import com.heima.model.wemedia.dtos.WmSensitiveChannelPageDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private WmChannelMapper wmChannelMapper;

    /**
     * 获取所有频道列表
     * @return
     */
    @Override
    public ResponseResult channels() {
        List channels = JSON.parseObject(cacheService.get(WemediaConstants.CHANNEL_LIST), List.class);
        if (channels == null || channels.size() == 0) {
            channels = list(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getStatus, WmChannel.Status.START.getCode()));
            cacheService.set(WemediaConstants.CHANNEL_LIST, JSON.toJSONString(channels));
        }
        return ResponseResult.okResult(channels);
    }

    /**
     * 频道列表显示
     * @param wmSensitiveChannelPageDto
     * @return
     */
    @Override
    public ResponseResult channelList(WmSensitiveChannelPageDto wmSensitiveChannelPageDto) {
        if (wmSensitiveChannelPageDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmSensitiveChannelPageDto.checkParam();
        IPage page = new Page(wmSensitiveChannelPageDto.getPage(), wmSensitiveChannelPageDto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotEmpty(wmSensitiveChannelPageDto.getName())) {
            lambdaQueryWrapper.eq(WmChannel::getName, wmSensitiveChannelPageDto.getName());
        }
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);
        page(page, lambdaQueryWrapper);
        PageResponseResult responseResult = new PageResponseResult(wmSensitiveChannelPageDto.getPage(), wmSensitiveChannelPageDto.getSize(), (int) page.getTotal());
        return responseResult.ok(page.getRecords());
    }

    /**
     * 频道保存
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult channelSave(WmChannel wmChannel) {
        if (wmChannel == null || wmChannel.getName() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel res = wmChannelMapper.exect(wmChannel.getName());
        if (res != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名重复");
        }
        if (wmChannel.getStatus() == null) {
            wmChannel.setStatus(true);
        }
        if (wmChannel.getOrd() == null) {
            List<WmChannel> list = list();
            wmChannel.setOrd(list == null ? 1 : list.size() + 1);
        }
        if (wmChannel.getIsDefault() == null) {
            wmChannel.setIsDefault(true);
        }
        save(wmChannel);
        cacheService.delete(WemediaConstants.CHANNEL_LIST);
        return ResponseResult.okResult(wmChannel);
    }

    /**
     * 频道更新
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult channelUpdate(WmChannel wmChannel) {
        if (wmChannel == null || StringUtils.isEmpty(wmChannel.getName()) || wmChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        if (wmChannel.getOrd() != null && wmChannel.getOrd() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "负数异常");
        }

        WmChannel res = wmChannelMapper.exect(wmChannel.getName());
        if (res != null && res.getId() != wmChannel.getId()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名重复");
        }

        if (!wmChannel.getStatus()) {
            Integer isExistChannel = wmNewsService.existOfChannel(wmChannel.getId());
            if (isExistChannel != null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道被引用，不能禁用");
            }
        }

        updateById(wmChannel);
        cacheService.delete(WemediaConstants.CHANNEL_LIST);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = getById(id);
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "数据已启用，无法删除");
        }
        removeById(id);
        cacheService.delete(WemediaConstants.CHANNEL_LIST);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
